﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml;
using mal.balades.Business.DataModel;
using mal.balades.Business.Helper;
using mal.balades.Business.Log;

namespace mal.balades.Business.DataAccess
{
    public class FeedDataAccess : BaseDataAccess
    {
        #region Fields
        private List<Feed> feeds = null;
        #endregion

        #region Construction / Destruction
        public FeedDataAccess(BaladesDataContext dataContext)
            : base(dataContext)
        {

        }
        #endregion

        #region enum, struct, class

        #endregion

        #region Properties
        private List<Feed> Feeds
        {
            get
            {
                if (feeds == null)
                {
                    feeds = DataContext.Feeds.ToList();
                }

                return feeds;
            }
        }
        #endregion

        #region Methods
        public IEnumerable<Feed> GetAllFeeds()
        {
            return this.Feeds;
        }

        //public void UpdateFeeds(IEnumerable<Feed> feeds)
        //{
        //    foreach (Feed feed in feeds)
        //    {
        //        Feed dbFeed = this.Feeds.FirstOrDefault(f => f.FeedId == feed.FeedId);

        //        if (dbFeed == null)
        //        {
        //            DataContext.Feeds.InsertOnSubmit(feed);
        //            this.Feeds.Add(feed);
        //        }
        //        else
        //        {
        //            if (!feed.IsDeleted)
        //            {
        //                dbFeed.Latitude = feed.Latitude;
        //                dbFeed.Longitude = feed.Longitude;
        //                dbFeed.Name = feed.Name;
        //                dbFeed.Url = feed.Url;
        //            }
        //            else
        //            {
        //                DataContext.Feeds.DeleteOnSubmit(dbFeed);
        //                this.Feeds.Remove(dbFeed);
        //            }
        //        }
        //    }
        //    DataContext.SubmitChanges();
        //}

        public IEnumerable<Feed> GetVisitFeeds(Visit visit)
        {
            List<Feed> results = new List<Feed>();

            try
            {
                
                List<Feed> feeds = this.Feeds.Where(f => f.Longitude.HasValue && f.Latitude.HasValue).ToList();

                if (visit.Latitude.HasValue && visit.Longitude.HasValue)
                {
                    var query = from feed in feeds
                                select new
                                {
                                    Feed = feed,
                                    Distance = GpsHelper.GetDistance(visit.Latitude.Value, visit.Longitude.Value
                                        , feed.Latitude.Value, feed.Longitude.Value)
                                };

                    results = (from f in query
                               where f.Distance < 20
                               orderby f.Distance
                               select f.Feed).ToList();
                }

                foreach (Feed feed in results)
                {
                    this.LoadFeedItemsFromDatabase(feed);
                }

                results = results.Where(f=>f.Items.Count>0).ToList();
            }
            catch
            {

            }

            return results;
        }

        public void LoadFeedItemsFromDatabase(Feed feed)
        {
            if (feed.Items == null)
            {
                feed.Items = feed.FeedItems.ToList();
            }
        }

        //public void UpdateFeedsItemsFromWeb()
        //{
        //    foreach (Feed feed in DataContext.Feeds)
        //    {
        //        List<FeedItem> feedItems = this.GetFeedItemsFromWeb(feed);

        //        if (feedItems != null)
        //        {
        //            feed.FeedItems.Clear();
        //            feed.LastUpdate = DateTime.Now;

        //            foreach (FeedItem item in feedItems)
        //            {
        //                feed.FeedItems.Add(item);
        //            }
        //        }
        //    }

        //    DataContext.SubmitChanges();
        //}

        //public List<FeedItem> GetFeedItemsFromWeb(Feed feed)
        //{
        //    try
        //    {
        //        MalLogManager.BusinessLogger.InfoFormat("Load feed {0}", feed.Url);

        //        XmlReader reader = XmlReader.Create(feed.Url);
        //       SyndicationFeed syndicationFeed = SyndicationFeed.Load(reader);
        //        List<FeedItem> feedItems = FeedItem.Convert(syndicationFeed.Items.ToList(), 5);

        //        return feedItems;
        //    }
        //    catch (Exception exp)
        //    {
        //        MalLogManager.BusinessLogger.Error(
        //                string.Format("Eror during load feed {0}, {1}", feed.Url, exp.Message), exp);
        //    }

        //    return null;
        //}
        #endregion

        #region Events

        #endregion
    }
}
